package com.bjsxt.livegoods.sendyzm.service.service.impl;

import com.bjsxt.livegoods.dao.user.UserDao;
import com.bjsxt.livegoods.entity.User;
import com.bjsxt.livegoods.sendyzm.service.service.SendyzmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Service
public class SendyzmServiceImpl implements SendyzmService {
    @Autowired
    private UserDao userDao;
    @Override
    public Map<String, Object> send(String phone) {
        //连接CMS短信服务平台,传递验证码
        //这里使用6个随机数模拟
        Random random=new Random();
        String str=""+random.nextInt(999999);
        //不足六位在前面补零
        if(str.length()<6){
            for(int i=0;i<6-str.length();i++){
                str="0"+str;
            }
        }
        //通过mongodb记录验证码
        User user=new User();
        user.setPhone(phone);
        user.setYzm(str);
        //系统记录当前的时间，就是生成验证码的时间
        user.setExpire(new Date());
        //保存验证码到数据库
        userDao.saveUser(user);
        //设置返回结果
        Map<String,Object> result=new HashMap<>();
        result.put("status",200);
        System.out.println("用户手机号:"+phone+",本次验证码，"+str);
        return result;
    }
}
